package cn.qcy;

import java.util.concurrent.locks.ReentrantLock;

public class Main {
    //true表示公平锁
    final static ReentrantLock reentrantLock = new ReentrantLock(true);
    public static void main(String[] args) {

        Thread thread1 = new Thread(()->{
                reentrantLock.lock();
                try {
                    System.out.println("thread1");
                } finally {
                    reentrantLock.unlock();
                }
            }
        );

        Thread thread2 = new Thread(()-> {
            if (reentrantLock.tryLock()){
                try {
                    System.out.println("thread2");
                } finally {
                    reentrantLock.unlock();
                }
            }else {
                System.out.println("thread2获取锁失败");
            }

        });

        Thread thread3 = new Thread(()->{
            try {
                reentrantLock.lockInterruptibly();
            } catch (InterruptedException e) {
                System.out.println("thread3获取锁失败 被打断了");
            }
        });

        //中断thread3
        Thread thread4 = new Thread(thread3::interrupt);



        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();


    }
}